/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2009 INRIA
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */
#ifndef SPIN_BARRIER_H
#define SPIN_BARRIER_H

namespace ns3 {

struct GlobalSpinBarrierContext 
{
  int sense;
};

class GlobalSpinBarrierImpl
{
 public:
  GlobalSpinBarrierImpl (int n);
  ~GlobalSpinBarrierImpl ();

  void Initialize (struct GlobalSpinBarrierContext *context);

  void Wait (struct GlobalSpinBarrierContext *context);
private:
  int m_totalWaiters;
  int m_waiters;
  int m_release;
};

struct TreeSpinBarrierContext
{
  int node;
  int sense;
};

class TreeSpinBarrierImpl
{
public:
  TreeSpinBarrierImpl (int n);
  ~TreeSpinBarrierImpl ();
  void Initialize (struct TreeSpinBarrierContext *context);

  void Wait (struct TreeSpinBarrierContext *context);
private:
  void WaitAux (int sense, struct TreeSpinBarrierNode *node);
  int m_totalWaiters;
  int m_contextsInitialized;
  struct TreeSpinBarrierNode *m_nodes;
};

} // namespace ns3

#endif /* SPIN_BARRIER_H */
